package com.iss.okair.service.assets;

import com.iss.common.StringTools;
import com.iss.common.UuidTools;
import com.iss.okair.db.entity.AssetsCar;
import com.iss.okair.db.entity.AssetsFinance;
import com.iss.okair.db.entity.Assetstrans;
import com.iss.okair.db.mapper.AssetstransMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AssetstransService {
    @Resource
    private AssetstransMapper mapper;

    public boolean add(Assetstrans trans){
        if (StringTools.isNullOrEmpty(trans.getId())) {
            trans.setBatchno(this.batch(trans.getAssetsid()));
            trans.setId(UuidTools.buildUUID());
            trans.setTranstype("1");
        int temp = mapper.add(trans);
        return temp>=1?true:false;
        }else {
            return false;
        }
    }
    //编写代码为batch
    private String batch(String str){
        AssetsCar finance= mapper.instocks(str);
        String str1=finance.getAssetscode();
        String temp= String.format("%s-%s",str1,System.currentTimeMillis());
        return  temp;
    }
    //领用和转移操作
    public boolean Use(String type,Assetstrans finance){
        //领用动作,出库

        if ("2".equals(type)){
            finance.setTranstype(type);
            return  mapper.add(finance)>=1 ? true:false;
        }else if("4".equals(type)) {
            //转移动作
            finance.setTranstype(type);
            return mapper.add(finance)>=1 ? true:false;
        }else {
            return false;
        }
    }
}
